package com.lantosec.hmrp.modules.category.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.util.Calculateutils;
import com.lantosec.hmrp.modules.category.entity.AssetsCheckDetailed;
import com.lantosec.hmrp.modules.category.entity.AssetsCheckNote;
import com.lantosec.hmrp.modules.category.entity.AssetsInfo;
import com.lantosec.hmrp.modules.category.mapper.AssetsCheckDetailedMapper;
import com.lantosec.hmrp.modules.category.service.IAssetsCheckDetailedService;
import com.lantosec.hmrp.modules.category.service.IAssetsCheckNoteService;
import com.lantosec.hmrp.modules.category.service.IAssetsInfoService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 盘点计划明细 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-04-01
 */
@Service
@Primary
@Log4j2
@SuppressWarnings("all")
public class AssetsCheckDetailedServiceImpl extends ServiceImpl<AssetsCheckDetailedMapper, AssetsCheckDetailed> implements IAssetsCheckDetailedService {

    @Autowired
    private IAssetsInfoService infoService;

    @Autowired
    private IAssetsCheckNoteService checkNoteService;

    /*@Autowired
    private IAssetsCheckPlanService checkPlanService;*/

    @Override
    public Page<AssetsCheckDetailed> planDetails(PageVO<AssetsCheckDetailed> page, String planId,String planType) {
        Integer count = 0;
        Integer assetsCount = 0;
        PageVO<AssetsCheckDetailed> result = null;
        if("1".equals(planType)){
             result = baseMapper.selectPage(page, new QueryWrapper<AssetsCheckDetailed>()
                    .eq(String.valueOf(AssetsCheckDetailed.ColName.plan_id), planId)
                    .groupBy(String.valueOf(AssetsCheckDetailed.ColName.area_id)));
             if(result != null){
                 //区域进度
                 result = areaSchedule(planId, result);
             }
        }else{
                result = baseMapper.selectPage(page,new QueryWrapper<AssetsCheckDetailed>()
                        .eq(String.valueOf(AssetsCheckDetailed.ColName.plan_id),planId)
                        .groupBy(String.valueOf(AssetsCheckDetailed.ColName.dept_id)));
                if(result != null){
                    //部门进度
                    result = deptSchedule(planId, result);
                }
        }
        return page.setRecords(result.getRecords());
    }

    private PageVO<AssetsCheckDetailed> deptSchedule(String planId, PageVO<AssetsCheckDetailed> result) {
        Integer count;
        Integer assetsCount;
        for (AssetsCheckDetailed it : result.getRecords()) {
            String deptId = it.getDeptId();
            //区域固定资产总数
            count = infoService.count(new QueryWrapper<AssetsInfo>()
                    .eq(String.valueOf(AssetsInfo.ColName.use_dept_id), deptId)
                    .notIn(String.valueOf(AssetsInfo.ColName.review_state),0));
            //已经盘点完成区域的固定资产总数
            assetsCount = checkNoteService.count(new QueryWrapper<AssetsCheckNote>()
                    .eq(String.valueOf(AssetsCheckNote.ColName.plan_id), planId)
                    .eq(String.valueOf(AssetsCheckNote.ColName.dept_id), deptId));
            if (count == 0 || assetsCount == 0) {
                it.setSchedule(0.0);
            } else {
                it.setSchedule( Calculateutils.roundRatio(assetsCount,count));
            }
        }
        return result;
    }

    private PageVO<AssetsCheckDetailed> areaSchedule(String planId, PageVO<AssetsCheckDetailed> result) {
        Integer count;
        Integer assetsCount;
        for (AssetsCheckDetailed it : result.getRecords()) {
            String areaId = it.getAreaId();
            //区域固定资产总数
            count = infoService.count(new QueryWrapper<AssetsInfo>()
                    .eq(String.valueOf(AssetsInfo.ColName.area_id), areaId));
            //已经盘点完成区域的固定资产总数
            assetsCount = checkNoteService.count(new QueryWrapper<AssetsCheckNote>()
                    .eq(String.valueOf(AssetsCheckNote.ColName.plan_id), planId)
                    .eq(String.valueOf(AssetsCheckNote.ColName.area_id), areaId));
            if (count == 0 || assetsCount == 0) {
                it.setSchedule(0.0);
            } else {
                Double b = Calculateutils.roundRatio(assetsCount,count);
                it.setSchedule(b);
            }
        }
        return result;
    }

    @Override
    public List<AssetsCheckDetailed> queryDeptOrAreaList(PageVO<AssetsCheckDetailed> page,String planId,String planType) {
        Integer checkCount;
        Integer assetsCount;
        PageVO<AssetsCheckDetailed> param = null;
        String categoryList = baseMapper.categoryList(planId);

        if("1".equals(planType)){
            param = baseMapper.selectPage(page, new QueryWrapper<AssetsCheckDetailed>()
                    .eq(String.valueOf(AssetsCheckDetailed.ColName.plan_id), planId)
                    .isNotNull("area_id"));
            for (AssetsCheckDetailed it : param.getRecords()) {
                String areaId = it.getAreaId();
                //区域固定资产总数
                assetsCount = baseMapper.assetsCount(areaId,categoryList);
                checkCount = baseMapper.queryAreaCount(planId,areaId,categoryList);
                if (checkCount == 0 || assetsCount == 0) {
                    it.setSchedule(0.0);
                } else {
                    double schedule = Calculateutils.roundRatio(checkCount, assetsCount);
                    it.setSchedule(schedule);
                }
                it.setAssetsCount(assetsCount);
                it.setCheckCount(checkCount);
            }

        }else{
            param = baseMapper.selectPage(page, new QueryWrapper<AssetsCheckDetailed>()
                    .eq(String.valueOf(AssetsCheckDetailed.ColName.plan_id), planId)
                    .isNotNull("dept_id"));
            for (AssetsCheckDetailed it : param.getRecords()) {
                String deptId = it.getDeptId();
                //部门固定资产总数
                assetsCount = baseMapper.assetsDeptCount(deptId,categoryList);

                //已经盘点完成部门的固定资产总数
                checkCount = baseMapper.queryDeptCount(planId,deptId,categoryList);
                /*checkCount = checkNoteService.count(new QueryWrapper<AssetsCheckNote>()
                        .eq(String.valueOf(AssetsCheckNote.ColName.plan_id), planId)
                        .eq(String.valueOf(AssetsCheckNote.ColName.dept_id), deptId));*/
                if (checkCount == 0 || assetsCount == 0) {
                    it.setSchedule(0.0);
                } else {
                    double schedule = Calculateutils.roundRatio(checkCount, assetsCount);
                    it.setSchedule(schedule);
                }
                it.setAssetsCount(assetsCount);
                it.setCheckCount(checkCount);
            }
        }
        return param.getRecords();
    }

    public static void main(String[] args) {
        Integer assetsCount = 4;
        Integer count = 247;
        Double d = (double)assetsCount / count * 100;
        System.out.println(d);
    }
}
